package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.mapper.bean.Auth;
import com.atguigu.crowd.mapper.bean.Role;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.service.api.AuthService;
import com.atguigu.crowd.service.api.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AssignHandler {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;

    //声明日志，方便打印调试
    private static final Logger logger = LoggerFactory.getLogger(AssignHandler.class);

    //根据adminID查询admin的角色分配情况
    @RequestMapping("/assign/to/assign/role/page.html")
    public String toAssignRolePage (@RequestParam("adminId")Integer adminId, ModelMap map) {

        // 1.根据adminID查询当前admin已分配的角色
        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);

        // 2.根据adminID查询当前admin未分配的角色
        List<Role> unAssignedRoleList = roleService.getUnAssignedRole(adminId);

        // 3.存入模型(本质上是request,setAttribute("attrName", attrValue),我们存入map模型后，springMVC替我们调request,setAttribute)
        map.addAttribute("assignedRoleList", assignedRoleList);
        map.addAttribute("unAssignedRoleList", unAssignedRoleList);

        logger.info("<===================成功返回角色信息===================>");
        return "assign-role";
    }

    //将前端选择好的角色信息保存至数据库中
    @RequestMapping("/assign/do/role/assign.html")
    public String saveAdminRoleRelationship (
            @RequestParam("adminId")Integer adminId,
            @RequestParam("pageNum")Integer pageNum,
            @RequestParam("keyword")String keyword,
            // 我们允许用户在页面上取消所有已分配角色再提交表单，所以可以不提供 roleIdList 请求参数
            // 设置 required=false 表示这个请求参数不是必须的
            @RequestParam(value = "roleIdList", required = false)List<Integer> roleIdList
    ) {
        logger.info("<===================开始保存角色信息===================>");
        adminService.saveAdminRoleRelationship(adminId, roleIdList);

        logger.info("<===================成功保存角色信息===================>");
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }


    //查询全部Auth数据
    @ResponseBody
    @RequestMapping("assgin/get/all/auth.json")
    public ResultEntity<List<Auth>> getAllAuth() {

        List<Auth> authList = authService.getAllAuth();

        return ResultEntity.successWithData(authList);
    }

    //查询已分配的Auth的id组成的数组
    @ResponseBody
    @RequestMapping("/assign/get/assigned/auth/id/by/role/id.json")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId (@RequestParam("roleId") Integer roleId) {

        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);

        return ResultEntity.successWithData(authIdList);
    }

    @ResponseBody
    @RequestMapping("/assign/do/role/assign/auth.json")
    public ResultEntity<String> saveRoleAuthRelationship (@RequestBody Map<String, List<Integer>> map) {

        authService.saveRoleAuthRelationship(map);
        return ResultEntity.successWithoutData();
    }





}